import { defineMock } from "../base";

// 局部变量存储时间设置信息
let timeSettings = {
  Zone: 8,
  Year: 2025,
  Month: 7,
  Day: 21,
  Hour: 12,
  Minute: 31,
  Sec: 32,
};

export default defineMock([
  // GET uri: /api/time/{cpu} 查询时间设置
  {
    url: "time/:cpu",
    method: ["GET"],
    body({ query, params }) {
      console.log("[MOCK] GET time/:cpu", query, params);
      return [{ ...timeSettings }];
    },
  },

  // POST uri: /api/time/{cpu} 整定时间设置
  {
    url: "time/:cpu",
    method: ["POST"],
    body({ query, params, body }) {
      console.log("[MOCK] POST time/:cpu", query, params, body);
      
      // 验证请求数据
      if (!Array.isArray(body) || body.length === 0) {
        return {
          error: "Invalid request data",
          message: "请求数据格式错误",
          code: 400,
        };
      }

      const timeData = body[0];
      
      // 验证时间数据
      if (
        typeof timeData.zone !== "number" ||
        typeof timeData.year !== "number" ||
        typeof timeData.month !== "number" ||
        typeof timeData.day !== "number" ||
        typeof timeData.hour !== "number" ||
        typeof timeData.minute !== "number" ||
        typeof timeData.sec !== "number"
      ) {
        return {
          error: "Invalid time data",
          message: "时间数据格式错误",
          code: 400,
        };
      }

      // 验证时间范围
      if (
        timeData.zone < -12 || timeData.zone > 12 ||
        timeData.year < 2000 || timeData.year > 2099 ||
        timeData.month < 1 || timeData.month > 12 ||
        timeData.day < 1 || timeData.day > 31 ||
        timeData.hour < 0 || timeData.hour > 23 ||
        timeData.minute < 0 || timeData.minute > 59 ||
        timeData.sec < 0 || timeData.sec > 59
      ) {
        return {
          error: "Invalid time range",
          message: "时间数据超出有效范围",
          code: 400,
        };
      }

      // 更新时间设置
      timeSettings = {
        Zone: timeData.zone,
        Year: timeData.year,
        Month: timeData.month,
        Day: timeData.day,
        Hour: timeData.hour,
        Minute: timeData.minute,
        Sec: timeData.sec,
      };

      return {
        cpu: parseInt(params.cpu),
        status: "success",
        message: "时间设置更新成功",
      };
    },
  },
]);
